package com.mr.service.impl;

import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import com.mr.service.MailService;
import com.mr.web.support.VelocityTemplate;
@Service("mailService")
public class MailServiceImpl implements MailService{
	private JavaMailSender javaMailSender;   
	private VelocityTemplate velocityTemplate;
	private String from;
	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public VelocityTemplate getVelocityTemplate() {
		return velocityTemplate;
	}

	public void setVelocityTemplate(VelocityTemplate velocityTemplate) {
		this.velocityTemplate = velocityTemplate;
	}

	public JavaMailSender getJavaMailSender() {
		return javaMailSender;
	}

	public void setJavaMailSender(JavaMailSender javaMailSender) {
		this.javaMailSender = javaMailSender;
	}
	
	public void sendEmail(String tomail, String subject, String text){   
        try {   
            MimeMessage msg = javaMailSender.createMimeMessage();   
            MimeMessageHelper message = new MimeMessageHelper(msg, true, "UTF-8");   
            message.setSubject(subject); 
            message.setFrom(from);
            message.setTo(tomail);   
            message.setText(text, true);
            javaMailSender.send(msg);   
        } catch (Exception me) {   
            me.printStackTrace();     
        }   
    }   
	public void sendEmail(String tomail, String subject, String template, Map model){   
        try {   
        	String text = velocityTemplate.parseTemplate(template, model);
        	sendEmail(tomail, subject, text); 
        } catch (Exception me) {   
            me.printStackTrace();     
        }   
    }
}
